/*   
 * Copyright (c) 2011, TUgether, TU Braunschweig
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 /**
 * @author Mohamed Karray <mohamed.karray@gmx.de>
 *
 */
 

package de.tu_bs.tugether.liferayunit.liferayElements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.javascript.host.Event;
import com.gargoylesoftware.htmlunit.javascript.host.KeyboardEvent;

import de.tu_bs.tugether.liferayunit.exceptions.LiferayElementNotFoundException;
import de.tu_bs.tugether.liferayunit.exceptions.LiferayUnitException;
import de.tu_bs.tugether.liferayunit.liferayPages.LiferayPage;

public class ChatWindow extends LiferayElement {

	protected static final String XPATH_KEY_OUTGOING_LIST = "outgoing-list";
	protected static final String XPATH_KEY_INCOMMING_LIST = "incomming-list";
	protected static final String XPATH_KEY_INPUT = "input";

	private List<ChatMessage> outgoingMessages;
	private List<ChatMessage> incommingMessages ;
	private HtmlTextArea chatField;

	public ChatWindow(HtmlElement html_Element, LiferayPage lfrPage) throws IOException,
			LiferayElementNotFoundException {
		super(html_Element, lfrPage);
	}

	@Override
	protected void initialize() throws LiferayElementNotFoundException {
		this.incommingMessages = new ArrayList<ChatMessage>();
		this.outgoingMessages = new ArrayList<ChatMessage>();
		try {
			System.out.println(this.getHtmlElement().asXml());
			this.chatField = this.<HtmlTextArea>getFirstByXPathKey(XPATH_KEY_INPUT);
		} catch (LiferayUnitException e) {
			e.printStackTrace();
		}
		try {
			List list = this.getByXPathKey(XPATH_KEY_INCOMMING_LIST);
			for (Object o :list){
				HtmlElement el;
				if (o instanceof HtmlElement){
					el = (HtmlElement) o;
					ChatMessage chatMessage;
					try {
						chatMessage = new ChatMessage(el, getLiferayPage());
						incommingMessages.add(chatMessage);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (LiferayUnitException  e) {
			e.printStackTrace();
		}
		try {
			List list = this.getByXPathKey(XPATH_KEY_OUTGOING_LIST);
			for (Object o :list){
				HtmlElement el;
				if (o instanceof HtmlElement){
					el = (HtmlElement) o;
					ChatMessage chatMessage;
					try {
						chatMessage = new ChatMessage(el, getLiferayPage());
						outgoingMessages.add(chatMessage);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (LiferayUnitException  e) {
			e.printStackTrace();
		}
	}

	public ChatMessage getLastOutgoingMessage(){
		int size = this.outgoingMessages.size();
		if ( size == 0 )
			return null;
		return this.outgoingMessages.get(size-1);
	}
	
	public ChatMessage getLastIncommingMessage(){
		int size = this.incommingMessages.size();
		if ( size == 0 )
			return null;
		return this.incommingMessages.get(size-1);
	}

	public List<ChatMessage> getOutgoingMessages() {
		return outgoingMessages;
	}

	public List<ChatMessage> getIncommingMessages() {
		return incommingMessages;
	}

	public void send(String message) {
		this.chatField.setTextContent(message);
		Event event = new KeyboardEvent(this.chatField,KeyboardEvent.TYPE_KEY_UP,13,false,false,false);
		this.chatField.fireEvent(event);
	}

}
